﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.DBAccess;

public static class CompanyRepository
{
    /// <summary>
    /// View dữ liệu trong table Patient
    /// </summary>
    public static DataSet LoadCompany()
    {
        DBAccessor db = null;
        DataSet ds = null;
        try
        {
            //string sql = "SELECT * FROM Patient";
            string sql = "SELECT ID, CompanyName FROM Company order by ID asc";

            db = new DBAccessor();
            ds = db.ExecuteQueryString(sql);

        }
        catch (Exception)
        {

        }
        finally
        {
            if (db != null) { db.Dispose(); }
        }
        return ds;
    }

    /// <summary>
    /// Kiểm tra folderPath này đã tồn tại trong CSDL chưa
    /// </summary>
    /// <param name="folderPath"></param>
    /// <returns></returns>
    public static bool IsExistFolderPath(string folderPath)
    {
        DBAccessor db = null;
        bool result = false;
        try
        {

            db = new DBAccessor();
            string sql = "SELECT * FROM Company where FolderPath = '" + folderPath + "'";
            DataSet ds = db.ExecuteQueryString(sql);
            if (ds.Tables[0].Rows.Count > 0)
                result = true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            if (db != null) { db.Dispose(); }
        }
        return result;
    }

    public static int ImportCompany(string companyName, string folderPath)
    {
        int companyId = 0;
        DBAccessor db = null;
        try
        {
            db = new DBAccessor();
            //===============================================
            //Kiểm tra thêm mới company
            if (!IsExistFolderPath(folderPath))
            {
                string insertSql = string.Format("insert into Company (CompanyName, FolderPath, CreatedDate) values (N\'{0}\',N\'{1}\',N\'{2}\')", companyName, folderPath, DateTime.Now);
                companyId = db.ExecuteNonQueryString(insertSql);
            }
            else //Nếu có Company thì update lại Name
            {
                string updateSql = string.Format("update Company set CompanyName = N\'{0}\' where FolderPath = N\'{1}\'", companyName, folderPath);
                companyId = db.ExecuteNonQueryString(updateSql);
            }

            //===============================================
            //Kiểm tra nếu admin chưa có thì thêm mới
            string sql = "SELECT * FROM Patient where upper(PatientName) = 'ADMIN' and CompanyId = " + companyId;
            DataSet ds = db.ExecuteQueryString(sql);
            if (ds.Tables[0].Rows.Count == 0)
            {
                PatientRepository.ImportPersonal("ADMIN" + companyId.ToString(), "ADMIN", companyId, folderPath);   
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            if (db != null) { db.Dispose(); }
        }

        return companyId;
    }
}
